//定义用户信息store
import { defineStore } from "pinia";

/**
 * 第一个参数为 名字
 * 第二个参数为 函数，函数的内部可以定义状态的所有内容
 */
export const useUserStore = defineStore('user',{
    state: () => ({
        user: {
            id: '',
            username: '',
            nickname: '',
            phone: '',
            email: '',
            role: '',
            avatar: '',
        }
    }),
    persist: true, // 持久化存储数据
    getters: {
        
    },
    actions: {
        getUserName() {
            return this.user.username
        },
        imSuperAdmin(){
            return this.user.username == 'admin'
        },
        imAdmin(){
            return this.user.role == 'ADMIN'
        },
        imUser(){
            return this.user.role == 'USER'
        },
        setUser(user) {
            this.user.id = user.id
            this.user.username = user.username
            this.user.nickname = user.nickname
            this.user.phone = user.phone
            this.user.email = user.email
            this.user.role = user.role
            this.user.avatar = user.avatar
        },
        getUser() {
            return this.user
        },
        setUserAvatar(avatar) {
            this.user.avatar = avatar
        }
    }
});